//
//  FMNavigationBar.m
//  FMLibrary
//
//  Created by cc on 13-9-3.
//  Copyright (c) 2013年 cc. All rights reserved.
//

#import "FMNavigationBar.h"

@implementation FMNavigationBar
@synthesize delegate = _delegate;
@synthesize backButton = _backButton;
@synthesize rightButton = _rightButton;



- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

-(void)awakeFromNib
{
    _buttons = [[NSMutableArray alloc] init];
    NSArray *sviews = [self subviews];
    for (int i=0; i<sviews.count; i++)
    {
        NSObject *child = [sviews objectAtIndex:i];
        if ([child isKindOfClass:[UIButton class]])
        {
            UIButton *btn = (UIButton*)child;
            [btn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
            [_buttons addObject:btn];
        }
    }
}

-(void)buttonAction:(id)sender
{
    UIButton *clicked_button = (UIButton *)sender;
    NSUInteger button_tag = clicked_button.tag;
    
    if ([_delegate respondsToSelector:@selector(fmNavigationBar:didSelectedAtTag:)])
    {
        [_delegate fmNavigationBar:self didSelectedAtTag:button_tag];
    }
}

-(NSString*)titleString
{
    return _titleLabel.text;
}

-(void)setTitleString:(NSString*)aTitleString
{
    _titleLabel.text = aTitleString;
    
//    CGRect r = [[UIScreen mainScreen] bounds];
//    
//    CGSize size = [aTitleString sizeWithFont:_titleLabel.font constrainedToSize:CGSizeMake(MAXFLOAT, _titleLabel.frame.size.height) lineBreakMode:UILineBreakModeWordWrap];
//    CGFloat w = size.width;
//    
//    CGFloat leftSpan = _backButton.bounds.origin.x + _backButton.bounds.size.width;
//    CGFloat rightSpan = r.size.width - _rightButton.bounds.origin.x;
//    
//    CGFloat maxWidth = leftSpan > rightSpan?leftSpan:rightSpan;
//    
//    [_titleLabel sizeThatFits:CGSizeMake(self.bounds.size.width - maxWidth*2 , self.bounds.size.height)];
//    _titleLabel.center = self.center;
}
@end
